import {Component, OnInit} from '@angular/core';
import {EventMessage, EventService, EventSource} from '../services/event.service';

import {RequestReplyService} from '../services/request-reply.service';
import {BookedResult} from '../entity/TT';

@Component({
  selector: 'app-schedule',
  templateUrl: './schedule.component.html',
  styleUrls: ['./schedule.component.scss']
})
export class ScheduleComponent implements OnInit {

  eventSource: EventSource = EventSource.SCHEDULE_CALENDAR;
  result: BookedResult = BookedResult.booked;

  activedDay: any;

  constructor(
    private eventService: EventService,
    private requestService: RequestReplyService
  ) {

  }

  ngOnInit() {
    this.eventService.subscribeEvent((evt: EventMessage) => {
      const src = evt.source;
      if (src == this.eventSource) {
        this.activedDay = evt.data;
        this.loadBookedList( this.activedDay );
      }
    });
  }


  loadBookedList(day: any): void {
    const p = {day: new Date(day.localDate ).toISOString(), result: this.result };
    this.requestService.GetBookedList(p)
    .subscribe(bookedList => {
        day.bookedList = bookedList;
    });
  }

}
